package com.imay.live.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.imay.base.adapter.RecyclingPagerAdapter;
import com.imay.base.util.StringUtil;
import com.imay.live.R;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by jeaman on 2017/3/20.
 */

public class ChangeRoomAdapter1 extends RecyclingPagerAdapter {
    private Context mContext;
    private boolean mLoopMode;  //是否无限循环
    private List<RoomInfoModel> mDataList = new ArrayList<>();
    private SparseArray<View> mViewList = new SparseArray<>();

    public ChangeRoomAdapter1(Context context) {
        this(context, true);
    }

    public ChangeRoomAdapter1(Context context, boolean loopMode) {
        mContext = context;
        mLoopMode = loopMode;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        ViewHolder viewHolder;
        if (convertView == null) {
            FrameLayout rootView = new FrameLayout(mContext);
            ImageView imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            rootView.addView(imageView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            viewHolder = new ViewHolder();
            viewHolder.rootView = rootView;
            viewHolder.coverIv = imageView;
            convertView = rootView;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        mViewList.put(getViewPosition(position), viewHolder.rootView);
        if (viewHolder.rootView.getVisibility() != View.VISIBLE) {
            viewHolder.rootView.setVisibility(View.VISIBLE);
        }
        RoomInfoModel item = getItem(position);
        String coverUrl = StringUtil.isEmpty(item.getImgFace()) && item.getUser() != null ? item.getUser().getImgHead() : item.getImgFace();
        GlideUtil.loadBlurImage(viewHolder.coverIv, coverUrl, 14, 3, R.mipmap.bg_03_loadingmoren_img);
        return convertView;
    }

    @Override
    public int getCount() {
        if (mLoopMode) {
            //两个以上，无限滑动
            return mDataList.size() < 2 ? mDataList.size() : Short.MAX_VALUE;
        } else {
            return mDataList.size();
        }
    }

    public int getMiddlePosition() {
        return (getCount() / 2) - (getCount() / 2 % mDataList.size());
    }

    public void setNewData(List<RoomInfoModel> dataList) {
        mDataList.clear();
        mDataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void setViewVisibility(int position, boolean visible) {
        int visibility = visible ? View.VISIBLE : View.GONE;
        View view = mViewList.get(getViewPosition(position));
        if (view != null && view.getVisibility() != visibility) {
            view.setVisibility(visibility);
        }
    }

    public RoomInfoModel getItem(int position) {
        int index;
        if (mLoopMode) {
            index = position % mDataList.size();
        } else {
            index = position;
        }
        return mDataList.get(index);
    }

    //缓存view位置，viewpager会缓存3个view，所以2个data需要特殊处理
    private int getViewPosition(int position) {
        if (mLoopMode) {
            int size = mDataList.size();
            if (size <= 2) {
                size = size * 3;
            }
            return position % size;
        } else {
            return position;
        }
    }

    private class ViewHolder {
        private ViewGroup rootView;
        private ImageView coverIv;
    }
}
